﻿using IH.Apis.Filters;
using IH.Comm.Dtos;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace IH.Apis.Controllers
{
    [ApiController]
    [Route("[controller]")]
    [ServiceFilter(typeof(TokenAuthAttribute)), ServiceFilter(typeof(CusExceptionAttribute))]
    public abstract class CoreController : ControllerBase
    {
        protected AppUser CurrentUser 
        { 
            get 
            {
                var identity = this.User.Identity;
                var claims = this.User.Claims;

                if (identity != null && identity.IsAuthenticated)
                {
                    return new AppUser()
                    {
                        Token = claims.FirstOrDefault(a => a.Type == nameof(AppUser.Token)).Value,
                        UserId= claims.FirstOrDefault(a => a.Type == nameof(AppUser.UserId)).Value,
                        HeadPic = claims.FirstOrDefault(a => a.Type == nameof(AppUser.HeadPic)).Value,
                        Name = claims.FirstOrDefault(a => a.Type == nameof(AppUser.Name)).Value,
                    };
                }

                return null;
            }
        }
    }
}
